#!/bin/bash
# 使用系统默认的 sh 运行
#################################################################################################################
# 作者：gfdgd xi
# 版本：3.0.0
# 更新时间：2022年10月02日
# 感谢：感谢 wine、deepin-wine 以及星火团队，提供了 wine、deepin-wine、spark-wine-devel 给大家使用，让我能做这个程序
# 基于 sh
#################################################################################################################
# 非强制性的必应组件，所以成功不成功都行
# 程序版本号
version=3.4.1
echo 安装组件
#python3 -m pip install --upgrade pyqt5 --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple > /dev/null 2>&1 | true
#python3 -m pip install --upgrade PyQtWebEngine --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple > /dev/null 2>&1 | true
python3 -m pip install --upgrade requests --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple --break-system-packages > /dev/null 2>&1 | true
python3 -m pip install --upgrade psutil --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple --break-system-packages > /dev/null 2>&1 | true
# termux 在使用 pip 安装 dbus-python 时会卡住
# python3 -m pip install --upgrade dbus-python --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple --break-system-packages > /dev/null 2>&1 | true  
python3 -m pip install --upgrade pyperclip --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple --break-system-packages > /dev/null 2>&1 | true
python3 -m pip install --upgrade pynput --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple --break-system-packages > /dev/null 2>&1 | true
python3 -m pip install --upgrade xpinyin --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple --break-system-packages > /dev/null 2>&1 | true
# 用于解决老版本 pip 没 --break-system-packages 参数的问题
python3 -m pip install --upgrade pynput --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple > /dev/null 2>&1 | true
python3 -m pip install --upgrade xpinyin --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple > /dev/null 2>&1 | true
python3 -m pip install --upgrade requests --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple > /dev/null 2>&1 | true
python3 -m pip install --upgrade psutil --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple > /dev/null 2>&1 | true
# termux 在使用 pip 安装 dbus-python 时会卡住
# python3 -m pip install --upgrade dbus-python --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple > /dev/null 2>&1 | true
python3 -m pip install --upgrade pyperclip --trusted-host https://repo.huaweicloud.com -i https://repo.huaweicloud.com/repository/pypi/simple > /dev/null 2>&1 | true
# 修改 box86/64 国内源错误数据
if [ -f /etc/apt/sources.list.d/box64.list ]; then
	sed -i 's/http:\/\/seafile.jyx2048.com:2345/http:\/\/gfdgdxi.v5.idcfengye.com/g' /etc/apt/sources.list.d/box64.list
fi
if [ -f /etc/apt/sources.list.d/box86.list ]; then
	sed -i 's/http:\/\/seafile.jyx2048.com:2345/http:\/\/gfdgdxi.v5.idcfengye.com/g' /etc/apt/sources.list.d/box86.list
fi
echo 执行完成
echo 移除旧组件
if [ -d /opt/apps/deepin-wine-runner/arm-package ]; then
	rm -rf /opt/apps/deepin-wine-runner/arm-package
fi
if [ -d /opt/apps/deepin-wine-runner/dlls-arm ]; then
	rm -rf /opt/apps/deepin-wine-runner/dlls-arm
fi
if [ -d /opt/apps/deepin-wine-runner/exa ]; then
	rm -rf /opt/apps/deepin-wine-runner/exa
fi
if [ -d /opt/apps/deepin-wine-runner/dxvk ]; then
	rm -rf /opt/apps/deepin-wine-runner/dxvk
fi
echo 移除完成
# 如果为非 X86 PC，可以删除掉一些无用组件（主要是用不了）
if [[ `arch` != "x86_64" ]]; then
	echo 非X86架构，删除对非X86架构无用的组件
	# 删除虚拟机功能
	#rm -rf /opt/apps/deepin-wine-runner/StartVM.sh
	#rm -rf /opt/apps/deepin-wine-runner/RunVM.sh
	#rm -rf /opt/apps/deepin-wine-runner/VM
	#rm -rf /usr/share/applications/spark-deepin-wine-runner-control-vm.desktop
	#rm -rf /usr/share/applications/spark-deepin-wine-runner-start-vm.desktop
	# 删除安装 wine 功能
	rm -rf "/opt/apps/deepin-wine-runner/wine install"
	# 这个注释掉的理论可用，不移除
	#rm -rf "/opt/apps/deepin-wine-runner/wine"
	rm -rf /usr/bin/deepin-wine-runner-wine-installer
	rm -rf /usr/bin/deepin-wine-runner-wine-install-deepin23
	rm -rf /usr/bin/deepin-wine-runner-wine-install
	rm -rf /usr/bin/deepin-wine-runner-winehq-install
	rm -rf /opt/apps/deepin-wine-runner/InstallWineOnDeepin23.py
	rm -rf /opt/apps/deepin-wine-runner/sparkstore.list
	rm -rf /opt/apps/deepin-wine-runner/AllInstall.py
	rm -rf /opt/apps/deepin-wine-runner/InstallNewWineHQ.sh
fi
# 设置目录权限，让用户可读可写，方便后续删除组件
chmod 777 -R /opt/apps/deepin-wine-runner > /dev/null 2>&1 | true

# 向服务器返回安装数加1（不显示内容且忽略错误）
python3 /opt/apps/deepin-wine-runner/Download.py $version > /dev/null 2>&1 | true

if [[ ! -f /data/data/com.termux/files/home/.vnc/passwd ]] && [[ -f /data/data/com.termux/files/usr/bin/vncpasswd ]]; then
	echo 开始配置 VNCServer
	echo 接下来需要设置 VNCServer 的密码
	set +e
	while [[ 1 ]];
	do
    	vncpasswd
    	if [[ $? == 0 ]]; then
        	break
    	fi
    	echo 设置有误，需重新设置
    	sleep 1
	done
	echo 'Wine 运行器安装完成，如果没有设置 $DISPLAY 变量的情况下打开 Wine 运行器'
	echo '可以在浏览器输入网址 http://localhost:6080/vnc.html 远程访问'
	echo '或者用 VNC 远程工具输入 localhost:5 远程访问'

fi



